<?php
namespace app\job;

use app\common\model\Member;
use app\common\model\MemberClue;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;
use think\queue\Job;

class Clue extends BaseJob
{

    /**
     * 三方（微信）小程序转化回传
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        Log::info($data);
        try{
            $member_info = Member::get($data['member_id']);
            if ($member_info['source'] != 1 ){
                $job->delete();
            }else{
                //计算八天内时间
                $begin_time = time() - 8* 86400;
                $clue_info = MemberClue::get(['member_id'=>$data['member_id'],'createtime'=>['gt',$begin_time]]);
                if (empty($clue_info)){
                    $job->delete();
                }else{
                    $nonce = randomName(8);//生成随机数
                    $timestamp = time();//时间戳
                    $collect = Array((string)\think\Config::get('app.douyin_token'),(string)$nonce,(string)$timestamp);
                    usort($collect,'strcmp');
                    $signature = sha1(implode('',$collect));//生成签名
                    $url = "https://clue.oceanengine.com/outer/wechat/applet/token/1798540634504204?timestamp={$timestamp}&nonce={$nonce}&signature={$signature}";
                    $params['clue_token'] = $clue_info['clue_token'];
                    $params['union_id'] = $member_info['unionid'];
                    $params['open_id'] = $member_info['mini_openid'];
                    $params['event_type'] = $data['event_type'];
                    if (isset($data['props']))
                        $params['props'] = $data['props'];
                    $result = http_request($url,"POST",$params);
                    Log::info($result);
                }
            }
        }catch (Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            throw $e;
        }
    }









}